package com.df.happylearn.articles;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.df.happylearn.R;
import com.df.happylearn.model.ArticleInfo;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dingfeng on 2016/5/25.
 */
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.MyViewHolder> {

    private Context mContext;
    private List<ArticleInfo> mArticleList;

    public ArticleAdapter(Context context, List<ArticleInfo> list) {
        mContext = context;
        mArticleList = list;
    }

    @Override
    public int getItemCount() {
        return mArticleList.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_article_list, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final ArticleInfo articleInfo = mArticleList.get(position);
        // time
        setTime(holder, articleInfo, position);
        // head
        String url = articleInfo.mThumbnail;
        Picasso.with(mContext).load(url).placeholder(R.drawable.article_default).into(holder.article_img);
        // title
        String[] title = articleInfo.mTitle.split("_");
        holder.title.setText(title[0]);
        // article source
        if (articleInfo.mTitle.contains("_")) {
            holder.source.setText(articleInfo.mOriginal);
        }
        // set read and unread state
        if (articleInfo.isread == 1) {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.article_read));
        } else {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.article_unread));
        }
        // register click event
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    private void setTime(MyViewHolder holder, ArticleInfo articleInfo, int position) {
        Date date = new Date(Long.parseLong(articleInfo.mCtime) * 1000);
        if (position == 0) {
            holder.ctime.setVisibility(View.VISIBLE);
            SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
            holder.ctime.setText(sdf.format(date));
        } else {
            ArticleInfo articleInfoTwo = mArticleList.get(position - 1);
            Date dateTwo = new Date(Long.parseLong(articleInfoTwo.mCtime) * 1000);
            if (date.getMonth() == dateTwo.getMonth() && date.getDay() == dateTwo.getDay()) {
                holder.ctime.setVisibility(View.GONE);
            } else {
                holder.ctime.setVisibility(View.VISIBLE);
                SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
                holder.ctime.setText(sdf.format(date));
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView ctime;
        ImageView article_img;
        TextView title;
        TextView source;

        public MyViewHolder(View view) {
            super(view);
            ctime = (TextView) view.findViewById(R.id.ctime);
            article_img = (ImageView) view.findViewById(R.id.article_img);
            title = (TextView) view.findViewById(R.id.title);
            source = (TextView) view.findViewById(R.id.sourse);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickLitener(OnItemClickListener l) {
        this.mOnItemClickListener = l;
    }
}